import sbtcrossproject.CrossPlugin.autoImport.{crossProject, CrossType}

val nameStr = "metabo_clound_platform"
name := nameStr

version := "0.1"

scalaVersion := "2.13.1"

lazy val server = (project in file("server")).settings(commonSettings).settings(
  scalaJSProjects := Seq(client),
  name := nameStr,
  version := "1.0",
  pipelineStages in Assets := Seq(scalaJSPipeline),
  pipelineStages := Seq(digest, gzip),
  compile in Compile := ((compile in Compile) dependsOn scalaJSPipeline).value,
  libraryDependencies ++= Seq(
    "com.vmunier" %% "scalajs-scripts" % "1.1.4",
    guice,
    specs2 % Test,
    ws,
    ehcache,
    "com.typesafe.play" %% "play-slick" % "4.0.2",
    "com.typesafe.slick" %% "slick-codegen" % "3.3.2",
    "mysql" % "mysql-connector-java" % "5.1.25",
    "com.github.tototoshi" %% "slick-joda-mapper" % "2.4.1",
    "commons-io" % "commons-io" % "2.5",
    "org.apache.poi" % "poi-ooxml" % "4.1.2",
    "org.apache.commons" % "commons-math3" % "3.6.1",
    "com.aliyun" % "aliyun-java-sdk-core" % "3.7.1",
    "com.aliyun" % "aliyun-java-sdk-dysmsapi" % "1.1.0",
    "com.typesafe.play" %% "play-joda-forms" % "2.7.3",
    "org.apache.xmlgraphics" % "batik-codec" % "1.10",
    "org.zeroturnaround" % "zt-zip" % "1.11",
    "com.github.tototoshi" %% "scala-csv" % "1.3.6",
    "junit" % "junit" % "4.12" % Test,
    "org.apache.pdfbox" % "pdfbox" % "2.0.8",
    "org.apache.pdfbox" % "pdfbox-tools" % "2.0.8",
    "com.h2database" % "h2" % "1.4.199",
    "javax.xml.bind" % "jaxb-api" % "2.3.0",
    "org.apache.commons" % "commons-lang3" % "3.6",
    "org.scala-lang.modules" %% "scala-parallel-collections" % "0.2.0",
    "org.typelevel" %% "cats-core" % "2.0.0",
    "javax.mail" % "mail" % "1.4.7",
    "org.scala-lang" % "scala-reflect" % "2.13.1",
    "org.postgresql" % "postgresql" % "42.2.5",
    "com.github.tminglei" %% "slick-pg" % "0.19.0",
    "com.github.tminglei" %% "slick-pg_joda-time" % "0.19.0",
    "com.github.tminglei" %% "slick-pg_play-json" % "0.19.0",
    "com.github.tminglei" %% "slick-pg_jts" % "0.19.0",
    "com.github.julien-truffaut" %% "monocle-core" % "2.0.3",
    "com.github.julien-truffaut" %% "monocle-macro" % "2.0.3",
    "org.scalatestplus.play" %% "scalatestplus-play" % "5.1.0" % Test,
    "org.scala-graph" %% "graph-core" % "1.13.5",
    "com.chuusai" %% "shapeless" % "2.3.3",
    "io.scalaland" %% "chimney" % "0.6.2",
    "com.lihaoyi" %% "upickle" % "2.0.0",
    "com.lihaoyi" %% "ujson-play" % "2.0.0",
    "dev.optics" %% "monocle-macro" % "3.1.0",
    "com.softwaremill.quicklens" %% "quicklens" % "1.7.5",
  ),
  scalacOptions ++= {
    Seq(
      "-Ymacro-annotations"
    )
  },
  npmAssets ++= NpmAssets.ofProject(client) { nodeModules =>
    (nodeModules / "@fortawesome/fontawesome-free").allPaths // sbt 1.0.0+
  }.value
).enablePlugins(PlayScala, WebScalaJSBundlerPlugin).dependsOn(sharedJvm)

val v = new {
  val scalaJSReact = "2.1.1"
}

val reactVersion = "17.0.2"

lazy val client = (project in file("client")).settings(commonSettings).settings(
  scalaJSUseMainModuleInitializer := false,
  useYarn := true,
  libraryDependencies ++= Seq(
    "com.lihaoyi" %%% "scalatags" % "0.11.1",
    "com.github.japgolly.scalajs-react" %%% "core-bundle-cats_effect"  % "2.1.1",
    "com.github.japgolly.scalajs-react" %%% "extra" % "2.1.1",
    "com.github.japgolly.scalajs-react" %%% "extra-ext-monocle3" % v.scalaJSReact,
    "dev.optics" %%% "monocle-macro" % "3.1.0",
    "org.scala-graph" %%% "graph-core" % "1.13.3",
    "io.scalaland" %%% "chimney" % "0.6.2",
    "com.lihaoyi" %%% "upickle" % "2.0.0",
    "org.scala-js" %%% "scala-js-macrotask-executor" % "1.0.0",
    "dev.optics" %%% "monocle-macro" % "3.1.0",
    "com.softwaremill.quicklens" %%% "quicklens" % "1.7.5",
    "me.shadaj" %%% "slinky-readwrite" % "0.7.4"
  ),
  Compile / npmDependencies ++= Seq(
    "jquery" -> "3.5.0",
    "react" -> reactVersion,
    "@types/react" -> reactVersion,
    "react-dom" -> reactVersion,
    "file-loader" -> "6.2.0",
    "nprogress" -> "0.2.0",
    "bootstrap-sweetalert" -> "1.0.1",
    "layer-src" -> "3.5.1",
    "css-loader" -> "3.4.2",
    "webpack-merge" -> "4.2.2",
    "style-loader" -> "1.1.3",
    "html-webpack-plugin" -> "5.2.0",
    "react-router-dom" -> "6.2.1",
    "react-transition-group" -> "4.4.2",
    "cytoscape" -> "3.15.1",
    "react-select" -> "5.4.0",
    "react-hook-form" -> "7.42.0",
    "react-bootstrap" -> "2.8.0",
    "react-i18next" -> "11.16.2",
    "i18next" -> "21.6.14",
    "bootstrap-datepicker" -> "1.6.4",
    "@fortawesome/fontawesome-free" -> "5.15.1",
    "@hookform/resolvers" -> "2.8.5",
    "yup" -> "0.32.11",
    "react-contextmenu" -> "2.14.0",
    "react-draggable"->"4.4.5",
    "styled-components" -> "5.3.6"
  ),
  scalacOptions ++= {
    Seq(
      "-Ymacro-annotations",
    )
  },
  webpackBundlingMode := BundlingMode.LibraryAndApplication(),
  webpackConfigFile := Some(baseDirectory.value / "my.custom.webpack.config.js"),
  fastOptJS / webpackEmitSourceMaps := true,
  fullOptJS / webpackEmitSourceMaps := true,
).enablePlugins(ScalaJSBundlerPlugin).dependsOn(sharedJs)

lazy val shared = crossProject(JSPlatform, JVMPlatform)
  .crossType(CrossType.Pure)
  .in(file("shared"))
  .settings(commonSettings)
  .settings(
    scalacOptions ++= {
      Seq("-Ymacro-annotations")
    },
    libraryDependencies ++= Seq(
      "io.github.alexarchambault" %% "data-class" % "0.2.1",
      "io.scalaland" %% "chimney" % "0.6.2",
      "dev.optics" %% "monocle-macro" % "3.1.0",
      "com.lihaoyi" %% "upickle" % "2.0.0",
    )
  )

lazy val sharedJvm = shared.jvm
lazy val sharedJs = shared.js

lazy val commonSettings = Seq(
  scalaVersion := "2.13.1"
)

onLoad in Global := (onLoad in Global).value andThen { s: State => "project server" :: s }
